$(function(){
	var indexCll = $(".indexCll").find(".indexCl a");
	var dataId = indexCll.attr("data-id");
	//console.log("dataId:"+dataId)
	//获取当前分类是否包含子分类
	/*if(dataId!=null && dataId!=undefined){
		onGoodsClass(dataId);
	//如当前分类id为空，则查询上级分类包含的商品信息	
	}else{
		var class_id = $("#class_id").val();
		btnGoodsDatil(class_id);
	}*/
	/**********初始化进来查询全部数据*****************/
	var class_id = $("#class_id").val();
	btnGoodsClassAll(class_id);
	
	typefaceLength();
	$('.indexClick a').click(function(){
		$('.indexClick a').css({background:'#fff',color:'#333'})
		$(this).css({background:'rgba(202,14,84,1)',color:'#fff'});
	});
});
	var currPage = 2;
	/**点击分类触发事件*****/
	function onGoodsClass(id){
		$(".indexCli").css("margin-top","24.4rem");
		$("#btnIndexClick").show();
		//获取二级分类所包含的三级分类信息
		$.ajax({
			url : rootPath+"/goodsClass/findIndexByGoodsClass",
			type : "post",
			data : {"id":id},
			async: false,
			success: function(data,status){
				$('#btnIndexClick').empty();
				if(data.length>0){
					var html = "";
					$.each(data,function(i,result){
						html += "<a href='javascript:void(0);' onclick='btnGoodsDatil"+'("'+result.id+'")'+";' class='fl'>"+result.name+"</a>";
					});
				//	$('.indexCll .indexClick').append(html);
					$("#btnIndexClick").append(html);
				}else{
					$("#btnIndexClick").hide();
					$(".indexCli").css("margin-top","17.4rem");
				}
				/***获取当前分类所包含的商品信息********/
				$.ajax({
				//	url : rootPath+"/goods/queryGoodsByClassIdList",
					url : rootPath+"/goods/queryBySortList",
					type : "post",
					data : {"classId":id},
					async: false,
					success: function(data,status){
						var resultData = data.resultList;
						currPage = 2;
					//	alert(JSON.stringify(resultData))
						$('.homeGoods').empty();
						var optionHtml = optionByHtml(resultData);
						optionHtml += "<li class='pageRefresh' onclick='clickRefresh"+'("'+id+'")'+";'>点击加载更多<img src='"+rootPath+"/image/huan.png' alt='刷新' /></li> ";
						$(".homeGoods").append(optionHtml);
						initIndex();
						typefaceLength();
					},
					error : function(xhr ,status, e){
						alert("服务器出了问题，请检查！"+e);
					}
				});
			},
			error : function(xhr ,status, e){
				alert("服务器出了问题，请检查！"+e);
			}
			
		});
		
	}
	
	/***
	 * 触发三级分类查询包含商品列表信息
	 */
	function btnGoodsDatil(classId){
		$.ajax({
		//	url : rootPath+"/goods/queryGoodsByClassIdList",
			url : rootPath+"/goods/queryBySortList",
			type : "post",
			data : {"classId":classId},
			async: false,
			success: function(data,status){
				var resultData = data.resultList;
				currPage = 2;
				$('.homeGoods').empty();
				var optionHtml = optionByHtml(resultData);
				optionHtml+="<li class='pageRefresh' onclick='clickRefresh"+'("'+classId+'")'+";'>点击加载更多<img src='"+rootPath+"/image/huan.png' alt='刷新' /></li>";
				$(".homeGoods").append(optionHtml);
				typefaceLength();
			},
			error : function(xhr ,status, e){
				alert("服务器出了问题，请检查！"+e);
			}
		});
	}
	
	
	
	function initIndex(){
		$('.indexClick a').click(function(){
			$('.indexClick a').css({background:'#fff',color:'#333'})
			$(this).css({background:'rgba(202,14,84,1)',color:'#fff'});
		});
	}

	function typefaceLength(){
		var str = $('.moreWord').text().substr(0,13)+'...';
		$('.moreWord').text(str);
	}
	
	/***商品分页请求*****/
	function clickRefresh(classId){
		$.ajax({
			url : rootPath+"/goods/queryBySortList",
			type : "post",
			data : {"classId":classId,"currPage":currPage},
			async: false,
			success: function(data,status){
				var resultData = data.resultList;
				if(resultData!=null && resultData.length>0){
					currPage++;
					var optionHtml = optionByHtml(resultData);
					$(".pageRefresh").before(optionHtml);
					typefaceLength();
				}else{
					$(".pageRefresh").text("数据加载完毕！！！")
	        		$(".pageRefresh").removeAttr("onclick");  //移除事件
				}
			},
			error : function(xhr ,status, e){
				alert("服务器出了问题，请检查！"+e);
			}
		});
		
	}
	

	/***拼装html*****/
	function optionByHtml(resultData){
		var optionHtml = "";
		$.each(resultData,function(i,result){
			console.log("result.marketPrice:"+(Number(result.marketPrice)).toFixed(2))
			var marketPrice = (Number(result.marketPrice)).toFixed(2);
			var k = i+1;
			if(k%2!=0){
				optionHtml+="<li class='fl'> "
			}else{
				optionHtml+="<li class='fr'> "
			}	
			optionHtml+= "<a href='"+rootPath+"/goods/queryGoodsDetail?id="+result.id+"'> "
			 var picture = result.picture;
			 if(picture!=null){
				 optionHtml+=" <img src='"+baseFile+picture+"' alt=''/> "
			 }else{
				 optionHtml+=" <img src='"+rootPath+"/image/pic5.png' alt='' /> "
			 }
			 optionHtml+= "<p class='moreWord'>"+result.name+"</p> "
			 + "	</a> "
			 + "	<p> "
			 + "	<span style='float:right;margin-top:30px; font-size:16px;'>月销"+result.monthSaleMount+"笔</span> "
					 var proTitle = result.proTitle;
					 if(proTitle!=null && proTitle!="" ){
						 optionHtml+= " <a href='javascript:void(0);' style=' margin-left:-30px;'>"+result.proTitle+"</a> "
					 }
			optionHtml+= "	</p> "
			 + "	<p> <b>¥ "+marketPrice+"</b></p> " 
			
			 + "</li>";
		});
		return optionHtml;
	}
	

	
	/***初始化进来查询全部数据***************/
	function btnGoodsClassAll(id){
		/***获取当前分类所包含的商品信息********/
		$.ajax({
			url : rootPath+"/goods/queryBySortList",
			type : "post",
			data : {"classId":id},
			async: false,
			success: function(data,status){
				$("#btnIndexClick").hide();
				$(".indexCli").css("margin-top","17.4rem");
				var resultData = data.resultList;
				currPage = 2;
				$('.homeGoods').empty();
				var optionHtml = optionByHtml(resultData);
				optionHtml += "<li class='pageRefresh' onclick='clickRefresh"+'("'+id+'")'+";'>点击加载更多<img src='"+rootPath+"/image/huan.png' alt='刷新' /></li> ";
				$(".homeGoods").append(optionHtml);
				initIndex();
				typefaceLength();
				
			},
			error : function(xhr ,status, e){
				alert("服务器出了问题，请检查！"+e);
			}
		});
	}
	
	

